package com.bdqn.petmalldemo.controller;


import com.alibaba.fastjson.JSONObject;
import com.bdqn.petmalldemo.common.ResultCodeEnum;
import com.bdqn.petmalldemo.common.ReturnResult;
import com.bdqn.petmalldemo.common.TokenCache;
import com.bdqn.petmalldemo.entity.User;
import com.bdqn.petmalldemo.service.FollowService;
import com.bdqn.petmalldemo.utils.CommonUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@RestController
@RequestMapping("/follow")
public class FollowController {
    @Resource
    private FollowService followService;

    @ApiOperation(value = "添加收藏")
    @PostMapping(value = "/add")
    public ReturnResult addFollow(String token,String productId){
        //验证当前用户的登录状态
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        //从TokenCache里面获取当前用户的登录信息
        User user = JSONObject.parseObject(TokenCache.get(token), User.class);
        //在执行收藏之前,检查当前用户是否已经收藏过该商品
        boolean checkResult = followService.checkExist(user.getId(), productId);
        //如果已经收藏,返回已收藏的信息
        if (checkResult) {
            returnResult=ReturnResult.error().code(ResultCodeEnum.ALREADY_FAVORITED.getCode())
                    .msg(ResultCodeEnum.ALREADY_FAVORITED.getMsg());
        }
        followService.addFollow(user.getId(),productId);
        return returnResult;

    }
    @ApiOperation(value = "检查当前用户是否已经收藏过该商品")
    @PostMapping(value = "/checkExist")
    public ReturnResult checkExist(String token,String productId){
        ReturnResult returnResult=CommonUtils.checkUserLogin(token);
        User user = JSONObject.parseObject(TokenCache.get(token), User.class);
        boolean checkResult = followService.checkExist(user.getId(), productId);
        //将检查结果放到返回对象中
        returnResult.data(checkResult);
        return  returnResult;
    }

   @ApiOperation("查询当前用户收藏商品列表")
    @PostMapping(value = "/search")
    public ReturnResult search(String token,Integer currentPage){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        //先从tokenCache中获取当前用户信息
        User user = JSONObject.parseObject(TokenCache.get(token), User.class);
        Map<String,Object> map = followService.getFollowProduct(user.getId(),currentPage);
        return returnResult.data(map);
    }

    @ApiOperation(value = "根据收藏记录Id删除收藏")
    @PostMapping(value = "/delete")
    public ReturnResult deleteFollowById(String token,Long followId){
        ReturnResult returnResult=CommonUtils.checkUserLogin(token);
        followService.removeFollowById(followId);
        return returnResult;
    }

    @ApiOperation(value = "根据当前登录用户查询粉丝")
    @PostMapping(value = "/fans")
    public ReturnResult getFansByUserId(String token,Integer currentPage){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        User user = JSONObject.parseObject(TokenCache.get(token), User.class);
        Map<String, Object> pageResultMap = followService.getFollowUserBySellerId(user.getId(),currentPage);
        return returnResult.data(pageResultMap);
    }
}

